package com.fr.report.semantic;

import com.fr.base.ColumnRow;
import com.fr.base.Inter;
import com.fr.base.Style;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.util.ListMap;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.data.condition.CommonCondition;
import com.fr.data.condition.Condition;
import com.fr.data.condition.JoinCondition;
import com.fr.data.condition.ListCondition;
import com.fr.data.core.Compare;
import com.fr.report.CellElement;
import com.fr.report.DefaultCellElement;
import com.fr.report.TemplateReport;
import com.fr.report.cellElement.CellExpandAttr;
import com.fr.report.cellElement.Formula;
import com.fr.report.cellElement.TableDataColumn;
import com.fr.report.cellElement.core.DSColumn;
import com.fr.report.cellElement.core.FunctionGrouper;
import com.fr.report.cellElement.core.RecordGrouper;
import com.fr.report.core.AnDSColumn;
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class GroupWizardRpt extends AbstractWizardRpt
{
  private List indexGroupList = new ArrayList();
  private List wizardCellElementList = new ArrayList();

  public void addWizardCellElement(WizardCellElement paramWizardCellElement)
  {
    this.wizardCellElementList.add(paramWizardCellElement);
  }

  public int getWizardCellElementSize()
  {
    return this.wizardCellElementList.size();
  }

  public WizardCellElement getWizardCellElement(int paramInt)
  {
    return ((WizardCellElement)this.wizardCellElementList.get(paramInt));
  }

  public int indexOfWizardCellElement(Object paramObject)
  {
    return this.wizardCellElementList.indexOf(paramObject);
  }

  public void clearWizardCellElements()
  {
    this.wizardCellElementList.clear();
  }

  public void addIndexGroup(IndexGroup paramIndexGroup)
  {
    this.indexGroupList.add(paramIndexGroup);
  }

  public int getIndexGroupCount()
  {
    return this.indexGroupList.size();
  }

  public IndexGroup getIndexGroup(int paramInt)
  {
    return ((IndexGroup)this.indexGroupList.get(paramInt));
  }

  public void clearIndexGroups()
  {
    this.indexGroupList.clear();
  }

  public int indexOfGroupList(int paramInt)
  {
    int i = 0;
    for (int j = 0; j < this.indexGroupList.size(); ++j)
    {
      if (((IndexGroup)this.indexGroupList.get(j)).getIndex() == paramInt)
        return j;
      if (((IndexGroup)this.indexGroupList.get(j)).getIndex() > paramInt)
        ++i;
    }
    return (paramInt + i);
  }

  public void insertIntoReport(TemplateReport paramTemplateReport, ColumnRow paramColumnRow)
  {
    WizardCellElement localWizardCellElement1;
    Object localObject1;
    ListCondition localListCondition1;
    Object localObject2;
    Object localObject3;
    Object localObject4;
    Object localObject5;
    Object localObject6;
    Object localObject7;
    ListMap localListMap = new ListMap();
    HashMap localHashMap = new HashMap();
    for (int i = 0; i < this.wizardCellElementList.size(); ++i)
    {
      localWizardCellElement1 = (WizardCellElement)this.wizardCellElementList.get(i);
      localObject1 = localWizardCellElement1.getTableDataName();
      localListCondition1 = (ListCondition)localListMap.get(localObject1);
      if (localListCondition1 == null)
      {
        localListCondition1 = new ListCondition();
        localListMap.put(localObject1, localListCondition1);
      }
      Condition localCondition1 = localWizardCellElement1.getCondition();
      if (localCondition1 != null)
      {
        int i5;
        if (localCondition1 instanceof ListCondition)
        {
          localObject2 = (ListCondition)localCondition1;
          int i3 = ((ListCondition)localObject2).getJoinConditionCount();
          for (i5 = i3 - 1; i5 >= 0; --i5)
          {
            localObject4 = ((ListCondition)localObject2).getJoinCondition(i5);
            Condition localCondition2 = ((JoinCondition)localObject4).getCondition();
            if (localCondition2 instanceof CommonCondition)
            {
              localObject5 = (CommonCondition)localCondition2;
              String str3 = localWizardCellElement1.getTableDataName();
              String str4 = ((CommonCondition)localObject5).getColumnName();
              localObject6 = ((CommonCondition)localObject5).getCompare();
              localObject7 = ((Compare)localObject6).getValue();
              if (localObject7 instanceof AnDSColumn)
              {
                AnDSColumn localAnDSColumn = (AnDSColumn)localObject7;
                String str5 = localAnDSColumn.getDsName();
                TableDataColumn localTableDataColumn = localAnDSColumn.getColumn();
                ((CommonCondition)localObject5).setColumnName(TableDataColumn.getColumnName(localTableDataColumn));
                localAnDSColumn.setDsName(str3);
                localAnDSColumn.setColumn(TableDataColumn.createColumn(str4));
                ListCondition localListCondition2 = (ListCondition)localHashMap.get(str5);
                if (localListCondition2 == null)
                {
                  localListCondition2 = new ListCondition();
                  localHashMap.put(str5, localListCondition2);
                }
                localListCondition2.addCondition((Condition)localObject5);
                ((ListCondition)localObject2).removeJoinCondition(i5);
              }
            }
          }
        }
        localListCondition1.addJoinCondition(0, localCondition1);
      }
      localWizardCellElement1.setCondition(null);
    }
    for (i = 0; i < this.wizardCellElementList.size(); ++i)
    {
      localWizardCellElement1 = (WizardCellElement)this.wizardCellElementList.get(i);
      localObject1 = localWizardCellElement1.getTableDataName();
      localListCondition1 = (ListCondition)localListMap.get(localObject1);
      if (localListCondition1 != null)
      {
        localWizardCellElement1.setCondition(localListCondition1);
        if (localHashMap.get(localObject1) != null)
          localListCondition1.addCondition((Condition)localHashMap.get(localObject1));
        localListMap.remove(localObject1);
      }
    }
    i = paramColumnRow.getColumn();
    int j = paramColumnRow.getRow();
    int k = 0;
    for (int l = this.indexGroupList.size() - 1; l >= 0; --l)
    {
      int i12;
      localObject2 = (WizardCellElement)this.wizardCellElementList.get(((IndexGroup)this.indexGroupList.get(l)).getIndex());
      if (localObject2 == null)
        break label1432:
      localObject1 = ((WizardCellElement)localObject2).toCellElement(l + i, 1 + j, 1, 1 + k, 0);
      if ((localObject1 != null) && (((CellElement)localObject1).getValue() instanceof DSColumn))
      {
        DSColumn localDSColumn1 = (DSColumn)((CellElement)localObject1).getValue();
        String str1 = TableDataColumn.getColumnName(localDSColumn1.getColumn());
        paramTemplateReport.addCellElement(new DefaultCellElement(l + i, j, 1, 1, str1));
      }
      paramTemplateReport.addCellElement((CellElement)localObject1);
      int i4 = ((WizardCellElement)localObject2).getColumnWidth();
      paramTemplateReport.setColumnWidth(((CellElement)localObject1).getColumn(), i4);
      int i6 = Math.max(paramTemplateReport.getRowHeight(((CellElement)localObject1).getRow()), ((WizardCellElement)localObject2).getRowHeight());
      paramTemplateReport.setRowHeight(((CellElement)localObject1).getRow(), i6);
      localObject4 = (IndexGroup)this.indexGroupList.get(l);
      for (int i9 = 0; i9 < ((IndexGroup)localObject4).getIndexSummaryCount(); ++i9)
      {
        localObject5 = ((IndexGroup)localObject4).getIndexSummary(i9);
        int i11 = indexOfGroupList(((IndexSummary)localObject5).getIndex()) + i;
        i12 = 2 + j + k;
        localObject6 = null;
        switch (((IndexSummary)localObject5).getFunctionIndex())
        {
        case 1:
          localObject6 = new DefaultCellElement(i11, i12, new Formula("=SUM(" + BaseCoreUtils.convertIntToABC(i11 + 1) + (1 + j + 1) + ")"));
          break;
        case 2:
          localObject6 = new DefaultCellElement(i11, i12, new Formula("=COUNT(" + BaseCoreUtils.convertIntToABC(i11 + 1) + (1 + j + 1) + ")"));
          break;
        case 3:
          localObject6 = new DefaultCellElement(i11, i12, new Formula("=AVERGE(" + BaseCoreUtils.convertIntToABC(i11 + 1) + (1 + j + 1) + ")"));
          break;
        case 4:
          localObject6 = new DefaultCellElement(i11, i12, new Formula("=MAX(" + BaseCoreUtils.convertIntToABC(i11 + 1) + (1 + j + 1) + ")"));
          break;
        case 5:
          localObject6 = new DefaultCellElement(i11, i12, new Formula("=MIN(" + BaseCoreUtils.convertIntToABC(i11 + 1) + (1 + j + 1) + ")"));
        }
        if (localObject6 == null)
          break label1145:
        localObject7 = ((CellElement)localObject6).getCellExpandAttr();
        if (localObject7 == null)
        {
          localObject7 = new CellExpandAttr();
          ((CellElement)localObject6).setCellExpandAttr((CellExpandAttr)localObject7);
        }
        ((CellExpandAttr)localObject7).setLeftParentColumnRow(ColumnRow.valueOf(l + i, 1 + j));
        ((CellExpandAttr)localObject7).setLeftParentDefault(false);
        paramTemplateReport.addCellElement((CellElement)localObject6);
      }
      if (((IndexGroup)localObject4).getIndexSummaryCount() > 0)
      {
        i9 = l + i;
        int i10 = 2 + j + k;
        localObject1 = new DefaultCellElement(i9, i10, new Formula("=" + BaseCoreUtils.convertIntToABC(l + i + 1) + (j + 1) + " + " + "\"" + Inter.getLocText("Summary") + "\""));
        CellExpandAttr localCellExpandAttr = ((CellElement)localObject1).getCellExpandAttr();
        if (localCellExpandAttr == null)
        {
          localCellExpandAttr = new CellExpandAttr();
          ((CellElement)localObject1).setCellExpandAttr(localCellExpandAttr);
        }
        localCellExpandAttr.setLeftParentColumnRow(ColumnRow.valueOf(l + i, 1 + j));
        localCellExpandAttr.setLeftParentDefault(false);
        paramTemplateReport.addCellElement((CellElement)localObject1);
        for (i12 = i9 + 1; i12 < getWizardCellElementSize() + i; ++i12)
          if (paramTemplateReport.getCellElement(i12, i10) == null)
          {
            localObject1 = new DefaultCellElement(i12, i10);
            localObject6 = ((CellElement)localObject1).getCellExpandAttr();
            if (localObject6 == null)
            {
              localObject6 = new CellExpandAttr();
              ((CellElement)localObject1).setCellExpandAttr((CellExpandAttr)localObject6);
            }
            ((CellExpandAttr)localObject6).setLeftParentColumnRow(ColumnRow.valueOf(l + i, 1 + j));
            ((CellExpandAttr)localObject6).setLeftParentDefault(false);
            paramTemplateReport.addCellElement((CellElement)localObject1);
          }
        ++k;
      }
    }
    for (l = 0; l < this.wizardCellElementList.size(); ++l)
    {
      label1145: int i2 = indexOfGroupList(l);
      if ((-1 < i2) && (i2 < this.indexGroupList.size()))
        label1432: break label1673:
      WizardCellElement localWizardCellElement2 = (WizardCellElement)this.wizardCellElementList.get(l);
      localObject1 = localWizardCellElement2.toCellElement(i2 + i, 1 + j, 0);
      if ((localObject1 != null) && (((CellElement)localObject1).getValue() instanceof DSColumn))
      {
        DSColumn localDSColumn2 = (DSColumn)((CellElement)localObject1).getValue();
        localObject4 = new FunctionGrouper();
        ((FunctionGrouper)localObject4).setDivideMode(1);
        localDSColumn2.setGrouper((RecordGrouper)localObject4);
        String str2 = TableDataColumn.getColumnName(localDSColumn2.getColumn());
        paramTemplateReport.addCellElement(new DefaultCellElement(i2 + i, j, 1, 1, str2));
      }
      paramTemplateReport.addCellElement((CellElement)localObject1);
      int i7 = localWizardCellElement2.getColumnWidth();
      paramTemplateReport.setColumnWidth(((CellElement)localObject1).getColumn(), i7);
      int i8 = Math.max(paramTemplateReport.getRowHeight(((CellElement)localObject1).getRow()), localWizardCellElement2.getRowHeight());
      paramTemplateReport.setRowHeight(((CellElement)localObject1).getRow(), i8);
    }
    label1673: Iterator localIterator = paramTemplateReport.cellIterator();
    while (true)
    {
      do
      {
        if (!(localIterator.hasNext()))
          break label1754;
        localObject1 = (CellElement)localIterator.next();
      }
      while (localObject1 == null);
      localObject3 = ((CellElement)localObject1).getStyle();
      ((CellElement)localObject1).setStyle(((Style)localObject3).deriveBorder(1, Color.black, 1, Color.black, 1, Color.black, 1, Color.black));
    }
    for (int i1 = 0; i1 < this.wizardCellElementList.size(); ++i1)
    {
      label1754: localObject3 = (WizardCellElement)this.wizardCellElementList.get(i1);
      addWCelllElementCondition(paramTemplateReport, (WizardCellElement)localObject3);
    }
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isAttr())
    {
      clearIndexGroups();
      clearWizardCellElements();
    }
    if (paramXMLableReader.isChildNode())
    {
      Object localObject;
      String str = paramXMLableReader.getTagName();
      if ("IndexGroup".equals(str))
      {
        localObject = new IndexGroup();
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
        addIndexGroup((IndexGroup)localObject);
      }
      else if ("WizardCellElement".equals(str))
      {
        localObject = new WizardCellElement();
        paramXMLableReader.readXMLObject((XMLReadable)localObject);
        addWizardCellElement((WizardCellElement)localObject);
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    Object localObject;
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("GroupWizardRpt");
    for (int i = 0; i < getIndexGroupCount(); ++i)
    {
      localObject = getIndexGroup(i);
      paramXMLPrintWriter.startTAG("IndexGroup");
      ((IndexGroup)localObject).writeXML(paramXMLPrintWriter);
      paramXMLPrintWriter.end();
    }
    for (i = 0; i < getWizardCellElementSize(); ++i)
    {
      localObject = getWizardCellElement(i);
      paramXMLPrintWriter.startTAG("WizardCellElement");
      ((WizardCellElement)localObject).writeXML(paramXMLPrintWriter);
      paramXMLPrintWriter.end();
    }
    paramXMLPrintWriter.end();
  }
}